const db = require('../utils/db');

const user = {
    /**
     * 判断邮箱是否存在
     * true 存在
     * false 不存在
     */
    async isExist(data) {
        let sql = `
    SELECT * FROM user_info
    where email = '${data.email}' limit 1
    `;
        let result = await db.query(sql);
        if (Array.isArray(result) && result.length > 0) {
            result = result[0];
        } else {
            result = null;
        }
        return result;
    },
    /**
     *注册，插入数据
     *
     * @param {Object} data 表单对象
     * @returns 注册结果
     */
    async insertInfo(data) {
        let user_id = Date.now();
        let res = await db.insert('user_info', {
            user_id,
            email: data.email,
            password: data.passwordEncrypt
        });
        let res2 = await db.insert('user_detail_info', {
            user: user_id,
            nickname: data.nickname
        });
        return res;
    },
    /**
     *登录校验
     *
     * @param {Object} data
     * @returns true 登录成功
     */
    async signIn(data) {
        let sql = `
        SELECT * FROM user_info
        where email = '${data.email}' and password = '${data.passwordEncrypt}' limit 1
        `;
        let result = await db.query(sql);
        if (Array.isArray(result) && result.length > 0) {
            result = result[0];
        } else {
            result = null;
        }
        return result;
    },
    /**
     * 获取用户信誉积分 单一值
     */
    async getCreditSingle(data) {
        let sql = `
        SELECT * FROM credit_record 
        WHERE scoreer = '${data.scoreer}' 
        and owner = '${data.owner}' limit 1
       `;
        let result = await db.query(sql);
        if (Array.isArray(result) && result.length > 0) {
            result = result[0];
        } else {
            result = null;
        }
        return result;
    },

    /**
     * 获取用户信誉积分 平均值
     */
    async getCreditAverage(data) {
        let sql = `
        SELECT AVG(score) FROM credit_record 
        WHERE owner = '${data.user_id}' limit 1
       `;
        let result = await db.query(sql);
        if (Array.isArray(result) && result.length > 0) {
            result = result[0];
        } else {
            result = null;
        }
        return result;
    },

    /** 
     * 获取用户信息 
     **/
    async getInfoById(formData) {
        let sql1 = `
        SELECT * FROM user_info
        WHERE user_id = '${formData.user_id}' limit 1
        `;
        let sql2 = `
        SELECT * FROM user_detail_info
        WHERE user = '${formData.user_id}' 
        ORDER BY create_time DESC LIMIT 1 
        `;
        let res = {};
        let result1 = await db.query(sql1);
        let result2 = await db.query(sql2);
        if (Array.isArray(result1) && result1.length > 0) {
            res.email = result1[0].email;
            if (Array.isArray(result2) && result2.length > 0) {
                res.nickname = result2[0].nickname;
                res.picture_name = result2[0].picture_name;
                res.major = result2[0].major;
                res.grade = result2[0].grade;
                res.campus = result2[0].campus;
                res.phone = result2[0].phone;
            }
        } else {
            res = null;
        }
        return res;
    },
    /**
     * 修改用户信息
     */
    async modifyInfo(formData) {
        let result = await db.update('user_detail_info', {
            user: `${formData.user_id}`
        }, formData.data);
        return result;
    },
    /**
     * 上传用户头像
     */
    async upload_portrait(formData) {
        let result = await db.update('user_detail_info', {
            user: formData.user_id
        }, {
            picture_name: formData.fileName
        });
        return result;
    },
    /**
     * 添加用户收藏
     * 
     */
    async addCollect(formData) {
        let sql = `
        SELECT * FROM collect_record
        WHERE user_id = '${formData.user_id}'
        AND book_id = '${formData.book_id}'
        `;
        let res = await db.query(sql);
        let result = null;
        if (Array.isArray(res) && res.length === 0) {
            result = await db.insert('collect_record', formData);
        }
        return result;
    },
    /**
     * 查看用户收藏
     * 
     */
    async collect(formData) {
        let sql = `
        SELECT id,book_id FROM collect_record
        WHERE user_id = '${formData.user_id}' 
        `;
        let result = await db.query(sql);
        return result;
    },
    /**
     * 取消用户收藏
     * 
     */
    async removeCollect(formData) {
        let sql = `
        DELETE FROM collect_record 
        WHERE id = '${formData.id}' 
        `;
        let result = await db.query(sql);
        return result;
    },
}
module.exports = user;